package com.example.mobile0123456789hwh0325;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;


//字符串：“95+25=”
//字符串分割：“95”，“+”，“25”
//“95”->95数字  "25"->25数字 操作符+
//result=95+25
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
// 1、全局控件变量声明
    private TextView tvMainTitle;
    private EditText edtTxtMainShow;
    private Button btnMainBack;
    private Button btnMainDiv;
    private Button btnMainClc;
    private Button btnMainMul;
    private Button btnMainNum7;
    private Button btnMainNum8;
    private Button btnMainNum9;
    private Button btnMainSub;
    private Button btnMainNum4;
    private Button btnMainNum5;
    private Button btnMainNum6;
    private Button btnMainPersent;
    private Button btnMainNum1;
    private Button btnMainNum2;
    private Button btnMainNum3;
    private Button btnMainAdd;
    private Button btnMainNum0;
    private Button btnMainPoint;
    private Button btnMainEqu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        3、调用initView()
        initView();
        btnMainNum0.setOnClickListener(this);
        btnMainNum1.setOnClickListener(this);
        btnMainNum2.setOnClickListener(this);
        btnMainNum3.setOnClickListener(this);
        btnMainNum4.setOnClickListener(this);
        btnMainNum5.setOnClickListener(this);
        btnMainNum6.setOnClickListener(this);
        btnMainNum7.setOnClickListener(this);
        btnMainNum8.setOnClickListener(this);
        btnMainNum9.setOnClickListener(this);
        btnMainClc.setOnClickListener(this);
        btnMainBack.setOnClickListener(this);
        btnMainAdd.setOnClickListener(this);
        btnMainEqu.setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {
        Button btn=(Button)v;
        int id=btn.getId();
        switch (id){
            case R.id.btn_main_num0:
                edtTxtMainShow.append("0");
                break;
            case R.id.btn_main_num1:
                edtTxtMainShow.append("1");
                break;
            case R.id.btn_main_num2:
                edtTxtMainShow.append("2");
                break;
            case R.id.btn_main_num3:
                edtTxtMainShow.append("3");
                break;
            case R.id.btn_main_num4:
                edtTxtMainShow.append("4");
                break;
            case R.id.btn_main_num5:
                edtTxtMainShow.append("5");
                break;
            case R.id.btn_main_num6:
                edtTxtMainShow.append("6");
                break;
            case R.id.btn_main_num7:
                edtTxtMainShow.append("7");
                break;
            case R.id.btn_main_num8:
                edtTxtMainShow.append("8");
                break;
            case R.id.btn_main_num9:
                edtTxtMainShow.append("9");
                break;
            case R.id.btn_main_clc:
                edtTxtMainShow.setText(null);
                break;
            case R.id.btn_main_back:
//                退格  123  ——>12
                String msg=edtTxtMainShow.getText().toString();
                if(msg.length()>1){
                    edtTxtMainShow.setText(msg.substring(0,msg.length()-1));
                }
                break;
            case R.id.btn_main_add:
                edtTxtMainShow.append("+");
                break;

            case R.id.btn_main_equ:
                edtTxtMainShow.append("=");

                break;
        }

    }

//2、控件初始化方法initView()
    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        edtTxtMainShow = (EditText) findViewById(R.id.edtTxt_main_show);
        btnMainBack = (Button) findViewById(R.id.btn_main_back);
        btnMainDiv = (Button) findViewById(R.id.btn_main_div);
        btnMainClc = (Button) findViewById(R.id.btn_main_clc);
        btnMainMul = (Button) findViewById(R.id.btn_main_mul);
        btnMainNum7 = (Button) findViewById(R.id.btn_main_num7);
        btnMainNum8 = (Button) findViewById(R.id.btn_main_num8);
        btnMainNum9 = (Button) findViewById(R.id.btn_main_num9);
        btnMainSub = (Button) findViewById(R.id.btn_main_sub);
        btnMainNum4 = (Button) findViewById(R.id.btn_main_num4);
        btnMainNum5 = (Button) findViewById(R.id.btn_main_num5);
        btnMainNum6 = (Button) findViewById(R.id.btn_main_num6);
        btnMainPersent = (Button) findViewById(R.id.btn_main_persent);
        btnMainNum1 = (Button) findViewById(R.id.btn_main_num1);
        btnMainNum2 = (Button) findViewById(R.id.btn_main_num2);
        btnMainNum3 = (Button) findViewById(R.id.btn_main_num3);
        btnMainAdd = (Button) findViewById(R.id.btn_main_add);
        btnMainNum0 = (Button) findViewById(R.id.btn_main_num0);
        btnMainPoint = (Button) findViewById(R.id.btn_main_point);
        btnMainEqu = (Button) findViewById(R.id.btn_main_equ);
    }


}